<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	积分、余额日志
*/
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'返单表'),
);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$return['params']=array();
	//会员ID
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(!empty($_RQ['orderid'])){
		$params['orderid']=intval($_RQ['orderid']);
		$return['params']['orderid']=$_RQ['orderid'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	//时间范围
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$return['params']['createtime']=$time_range;
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getslice('jy_cun9fan_fanlog',$params,array($pindex,$psize),$total,array(),'','id desc');
	foreach($list as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$item['fandayrate']=$item['fandayrate']>0?$item['fandayrate']:$this->setting['fandayrate'];
		$nexttime=$item['uptime']+24*3600;
		if(!$item['status']){
			$item['nextday']=date('Y-m-d',$nexttime);
		}
		if($item['uptime']>time()){
			$item['isnostart']=1;
		}
		$onefanfee=$item['fandayrate']*$item['fanfee']/100;
		/*if($item['status']==1 && $item['lastfanfee']>$onefanfee){
			$lastfanlog=pdo_get('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'','id desc');
			if($lastfanlog['id']==$item['id']){
				pdo_update('jy_cun9fan_fanlog',array('status'=>0),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
				$item['status']=0;
			}
		}*/
		if(!$item['status'] && $item['lastfanfee']<1){
			pdo_update('jy_cun9fan_fanlog',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['id']));
			$item['status']=1;
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'orderid','title'=>'订单ID','type'=>'string'),
			array('name'=>'fanfee','title'=>'总返额','type'=>'string'),
			array('name'=>'fandayrate','title'=>'最大回补%','type'=>'unitstring','unit'=>'%'),
			array('name'=>'lastfanfee','title'=>'剩余返额','type'=>'string'),
			array('name'=>'nextday','title'=>'下次回补日期','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'change','title'=>'修改最大回补%','show'=>array('status'=>array(0,2))),
			array('type'=>'modal','op'=>'changelastfanfee','title'=>'修改剩余返额','show'=>array('status'=>array(0,2))),
			array('type'=>'href','op'=>'changenextday','title'=>'修改下次回补日期','show'=>array('status'=>array(0,2))),
			array('type'=>'modal','op'=>'huibu','title'=>'添加一笔分润','show'=>array('status'=>2)),
			array('type'=>'post','op'=>'openfan','title'=>'立即开始回补','show'=>array('status'=>0,'isnostart'=>1)),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'orderid','title'=>'订单ID'),
			array('type'=>'choose','width'=>15,'title'=>'会员id','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'time_range','width'=>18,'title'=>'创建时间','name'=>'createtime','help'=>''),
		),
	);
	$return['status']=$this->fanlog_status;
	exi($return);
}
if($_RQ['op']=='openfan'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	pdo_update('jy_cun9fan_fanlog',array('uptime'=>$daytime-1000),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$fannum = pdo_count('jy_cun9fan_fanlog',array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'uptime <'=>$daytime));
	if($fannum){
		$fandayfee=$dayallfee*$this->setting['fandayfee']/(100*$fannum);
    }
	$item['isfandayfee']=1;
	$this->model('commission')->up_fanone($item,$fandayfee);
	exi('操作成功！','','list');
}
if($_RQ['op']=='changenextday'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		pdo_update('jy_cun9fan_fanlog',array('uptime'=>strtotime($post['nextday'])-1000),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('修改成功！','','list');
	}
	$item=pdo_get('jy_cun9fan_fanlog',array('id'=>intval($_RQ['id'])),array('id','uptime'));
	if(!$item){
		exi('指定单不存在！','error');
	}
	$nexttime=$item['uptime']+24*3600;
	$item['nextday']=date('Y-m-d',$nexttime);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改下次回补日期',
		'edit'=>array(
			array('name'=>'nextday','title'=>'修改下次回补日期','type'=>'time','time_type'=>'day'),
		),
	);
	exi($return);
}
if($_RQ['op']=='changelastfanfee'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		pdo_update('jy_cun9fan_fanlog',array('lastfanfee'=>$post['lastfanfee']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('修改成功！','','list');
	}
	$item=pdo_get('jy_cun9fan_fanlog',array('id'=>intval($_RQ['id'])),array('id','lastfanfee'));
	if(!$item){
		exi('指定单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改剩余返额',
		'edit'=>array(
			array('name'=>'lastfanfee','title'=>'修改剩余返额','type'=>'unitstring','unit'=>'元'),
		),
	);
	exi($return);
}
if($_RQ['op']=='change'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		pdo_update('jy_cun9fan_fanlog',array('fandayrate'=>$post['fandayrate']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('修改最大回补成功！','','list');
	}
	$item=pdo_get('jy_cun9fan_fanlog',array('id'=>intval($_RQ['id'])),array('id','fandayrate'));
	if(!$item){
		exi('指定单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'修改最大回补',
		'edit'=>array(
			array('name'=>'fandayrate','title'=>'最大回补%','type'=>'unitstring','unit'=>'%'),
		),
	);
	exi($return);
}
if($_RQ['op']=='huibu'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if($post['jiafee']<=0){
			exi('请添加分润额度！','error');
		}
		$this->model('commission')->up_fanone($post,'',$post['jiafee'],1);
		exi('添加成功！','','list');
	}
	$item=pdo_get('jy_cun9fan_fanlog',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定单不存在！','error');
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'desc'=>'分润',
		'edit'=>array(
			array('name'=>'jiafee','title'=>'分润额度','type'=>'unitstring','unit'=>'元'),
		),
	);
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cun9fan_fanlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cun9fan_fanlog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}